package com.quarters.controller.admin;


import com.quarters.bean.UserInfo;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.commen.BaseController;
import com.quarters.util.Respons.ResponseUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-02
 */
@Controller
@RequestMapping("admin/userInfo")
public class UserInfoController extends BaseController {

    /**
     * @return 修改密码
     */
    @RequestMapping(value = "password.html",method = RequestMethod.GET)
    public String password(Model model,HttpSession session){
        model.addAttribute("userInfo",getUserInfo(session));
        return "admin/password";
    }

    /**
     * @return 跳转支付
     */
    @RequestMapping(value = "pay.html",method = RequestMethod.GET)
    public String pay(){
        return "pay";
    }
    /**
     * @return 跳转支付
     */
    @RequestMapping(value = "error.html",method = RequestMethod.GET)
    public String error(){
        return "error";
    }

    /**
     * @return 跳转后台登陆
     */
    @RequestMapping(value = "login.html",method = RequestMethod.GET)
    public String login(){
        return "admin/login";
    }
    /**
     * @return 跳转后台首页
     */
    @RequestMapping(value = "index.html",method = RequestMethod.GET)
    public String index(){
        return "admin/index";
    }
    /**
     * @return 跳转后台欢迎页
     */
    @RequestMapping(value = "welcome.html",method = RequestMethod.GET)
    public String welcome(){
        return "admin/welcome";
    }

    /**
     * @return 跳转后台用户管理
     */
    @RequestMapping(value = "userInfo.html",method = RequestMethod.GET)
    public String userInfo(Model model,HttpSession session){
        model.addAttribute("username",getUserInfo(session));


        return "admin/userInfo/userInfo";
    }
    /**
     * @return 跳转文章订单管理
     */
    @RequestMapping(value = "articleOrder.html",method = RequestMethod.GET)
    public String articleOrder(){
        return "admin/order/articleOrder";
    }
    /**
     * @return 跳转通知订单管理
     */
    @RequestMapping(value = "noticeOrders.html",method = RequestMethod.GET)
    public String noticeOrders(){
        return "admin/order/noticeOrders";
    }
    /**
     * @return 跳转后台用户添加
     */
    @RequestMapping(value = "userInfo_add.html",method = RequestMethod.GET)
    public String userInfo_add(Model model,HttpSession session){
        UserInfo userInfo = userInfoService.getById(getUserInfo(session).getId());
        model.addAttribute("yesAdmin",userInfo);
        return "admin/userInfo/userInfo_add";
    }

    /**
     * @return 小区管理
     */
    @RequestMapping(value = "quarters.html",method = RequestMethod.GET)
    public String quarters(){
        return "admin/quarters/quarters";
    }
    /**
     * @return 报修管理
     */
    @RequestMapping(value = "repair.html",method = RequestMethod.GET)
    public String repair(){
        return "admin/repair/repair";
    }

    /**
     * @return 保修查询
     */
    @RequestMapping(value = "inquiry.html",method = RequestMethod.GET)
    public String inquiry(){
        return "admin/inquiry/inquiry";
    }


    /**
     * @return 跳转用户管理
     */
    @RequestMapping(value = "user.html",method = RequestMethod.GET)
    public String user(){
        return "admin/user/user";
    }
    /**
     * @return 跳转文章管理
     */
    @RequestMapping(value = "article.html",method = RequestMethod.GET)
    public String article(){
        return "admin/article/article";
    }
    /**
     * @return 跳转审核
     */
    @RequestMapping(value = "examine.html",method = RequestMethod.GET)
    public String examine(){
        return "admin/examine/examine";
    }

    /**
     * @return 跳转通知
     */
    @RequestMapping(value = "notice.html",method = RequestMethod.GET)
    public String notice(){
        return "admin/notice/notice";
    }

    /**
     * @return 跳转通知审核
     */
    @RequestMapping(value = "noticeOrder.html",method = RequestMethod.GET)
    public String noticeOrder(){
        return "admin/noticeOrder/noticeOrder";
    }

    /**
     * @param session
     * @param username
     * @param password
     * @return 后台登陆
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public Map tologin(HttpSession session, String username, String password) {
        if(userInfoService.login(username,password,session)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.loginFail();
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<UserInfo> selectMyPage(VoPage voPage,HttpSession session ){

        return userInfoService.selectMyPage(voPage,getUserInfo(session).getId());
    }


    /**
     * @param username
     * @param password
     * @param menuId
     * @param quartersId
     * @param
     * @param session
     * @return 添加角色
     */
    @RequestMapping("/saveUserInfo")
    @ResponseBody
    public Map<String, Object> saveUserInfo(
            String username,String password,String menuId,String quartersId,String roleName,String openId, HttpSession session) {
        if(getUserInfo(session).getUsername().equals("admin")){
            return  userInfoService.saveUserInfo(username,password,menuId,quartersId,roleName,openId,getUserInfo(session).getId(),1);
        }
        return  userInfoService.saveUserInfo(username,password,menuId,quartersId,roleName,openId,getUserInfo(session).getId(),2);

    }


    /**
     * @param id
     * @return 删除角色
     */
    @RequestMapping(value = "deleteByIdUserInfo",method = RequestMethod.POST)
    @ResponseBody
    public Map deleteByIdUserInfo(Long id){
        return userInfoService.deleteByIdUserInfo(id);
    }


    /**
     * @param userInfo
     * @return 修改密码
     */
    @RequestMapping(value = "updatePassword",method = RequestMethod.POST)
    @ResponseBody
    public Map updatePassword(@RequestBody UserInfo userInfo){
        if(userInfoService.updateById(userInfo)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
}



}
